<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="${ctxPath!}/layui/css/layui.css" rel="stylesheet" type="text/css">
    <% include("/templates/common/dialog.html"){} %>
</head>
<div style="padding:10px 50px 10px 10px">
    <form id="userForm" method="post" action="${ctxPath!}/sys/user/edit" class="layui-form">
        <div style="margin-bottom:20px;font-size:14px;border-bottom:1px solid #ccc; padding-bottom: 5px;">用户信息</div>
        <input type="hidden" name="id" value="${user.id}"/>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名称</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="username" value="${user.username}" lay-verify="required" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户账号</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="account" value="${user.account}" lay-verify="required" />
            </div>
        </div>
        <% if(user.id == null || user.id <= 0){ %>
        <div class="layui-form-item">
            <label class="layui-form-label">用户密码</label>
            <div class="layui-input-block">
                <input class="layui-input" id="password" type="password" name="password" lay-verify="required" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input class="layui-input" id="r_password" type="password" name="r_password" lay-verify="required" />
            </div>
        </div>
        <% } %>
        <!--如果是机构管理员则显示相应角色,如果是超级管理员则显示机构,选择完机构之后再ajax回调显示相应角色-->
        <div class="layui-form-item">
            <label class="layui-form-label">用户角色</label>
            <div class="layui-input-block">
                <select name="rid" id="rid">
                    <option value=""></option>
                    <%
                    for(role in roles!){
                    %>
                    <option value="${role.id}" ${user.rid == role.id ? "selected" : ""}>${role.name}</option>
                    <%
                    }
                    %>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button id="submitButton" class="layui-btn" lay-submit lay-filter="*">保存</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="closeAll()">取消</button>
            </div>
        </div>
    </form>
</div>
<script src="${ctxPath!}/jquery/jquery.min.js"></script>
<script src="${ctxPath!}/layui/layui.js"></script>
<script src="${ctxPath!}/jqgrid/jquery.jqGrid.minffe4.js?0820"></script>
<script src="${ctxPath!}/jqgrid/myjqGrid.js?v=1.0.1"></script>
<script type="text/javascript">
    layui.use('form', function(){
        var form = layui.form;
        form.on('submit(*)', function(data){
            var userId = '${user.id}';
            if(!userId){
                if($('#password').val() != $('#r_password').val()){
                    layer.alert("两次输入的密码不相同", {
                        icon: 2,
                        skin: 'layer-ext-moon'
                    });
                    return false;
                }
            }
            $.post($("#userForm").attr("action"), $("#userForm").serialize(), function(data){
                if(data.status == 200){
                    layer.alert(data.msg, {
                        icon: 1,
                        skin: 'layer-ext-moon'
                    },function () {
                        parent.myJqGrid.reloadGrid({});
                        parent.layer.closeAll();
                    });
                }else{
                    layer.alert(data.msg, {
                        icon: 2,
                        skin: 'layer-ext-moon'
                    });
                }
            });
            return false; //阻止表单跳转。
        });
        form.on('select(orgId)', function(data){
            console.log(data.elem); //得到select原始DOM对象
            console.log(data.value); //得到被选中的值
            console.log(data.othis); //得到美化后的DOM对象
            $.post("/sys/role/getRoles",{"orgId":data.value},function(data){
                var html="";
                var roles=data.roles;
                for(var i=0;i<roles.length;i++){
                    html+='<option value="'+roles[i].id+'">'+roles[i].name+'</option>';
                }
                $("#rid").html(html);
                form.render('select'); //刷新select选择框渲染
            },"json");
        });
    });
    function closeAll() {
        parent.layer.closeAll();
    }
</script>